package com.zz.utils;

import cn.hutool.core.text.csv.CsvRow;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.zz.common.ZohoRecord;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
@Component
public class BUtils {

    @Autowired
    ZohoRecord zohoRecord;

    @Autowired
    BulkUtil BulkUtil;


    //查询合同目标医院
    public List<JSONObject> getContractTargetHospital(String token, String contractId) {
        String query = "{\n" +
                "    \"query\": {\n" +
                "        \"module\": {\n" +
                "            \"api_name\": \"Contract_Target_Hospital\"\n" +
                "        },\n" +
                "        \"fields\": [\n" +
                "            \"id\",\n" +
                "            \"Target_Hospital_c\",\n" +
                "            \"Institutional_c\",\n" +
                "            \"Hospital_Integral_c\",\n" +
                "            \"Hospital_Status_c\",\n" +
                "            \"Product_Sequrity_Deposit_c\",\n" +
                "            \"ExecutivePrice_c\",\n" +
                "            \"Target_Quantity_c\",\n" +
                "            \"DevDeadline_c\",\n" +
                "            \"Target_Hospital_c.Name\",\n" +
                "            \"Target_Hospital_c.HospitalList_c.Name\"\n" +
                "        ],\n" +
                "        \"criteria\": {\n" +
                "            \"field\": {\n" +
                "                \"api_name\": \"Contract_c\"\n" +
                "            },\n" +
                "            \"comparator\": \"equal\",\n" +
                "            \"value\": " + contractId + "\n" +
                "        }\n" +
                "    }\n" +
                "}";
        log.info("查询合同目标医院");
        List<CsvRow> data = BulkUtil.getBulkCSVData(token, query, new ArrayList<>());

        List<JSONObject> result_JsonObjects = new ArrayList<>();
        for (CsvRow csvRow : data) {
            if (StrUtil.isNotBlank(csvRow.get(0)) && !"Id".equals(csvRow.get(0))) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("id", csvRow.get(0));
                JSONObject temp = new JSONObject();
                temp.put("id", csvRow.get(1));
                jsonObject.put("Target_Hospital_c", temp);
                jsonObject.put("Institutional_c", csvRow.get(2));
                jsonObject.put("Hospital_Integral_c", csvRow.get(3));
                jsonObject.put("Hospital_Status_c", csvRow.get(4));
                jsonObject.put("Product_Sequrity_Deposit_c", csvRow.get(5));
                jsonObject.put("ExecutivePrice_c", csvRow.get(6));
                jsonObject.put("Target_Quantity_c", csvRow.get(7));
                jsonObject.put("DevDeadline_c", csvRow.get(8));
                jsonObject.put("HospitalList_c", csvRow.get(10));
                result_JsonObjects.add(jsonObject);
            }
        }

        return result_JsonObjects;
    }

    //查询目标医院 批量
    public List<JSONObject> getTargetHospitalBatch(String token,Set<String> ids) {
        String idsStr = ids.stream()
                .map(id -> "\"" + id + "\"")  // 为每个 id 添加引号
                .collect(Collectors.joining(", ")); // 用逗号连接
        String query = "{\n" +
                "    \"query\": {\n" +
                "        \"module\": {\n" +
                "            \"api_name\": \"TargetHospital\"\n" +
                "        },\n" +
                "        \"fields\": [\n" +
                "            \"id\",\n" +
                "            \"HospitalList_c.Name\",\n" +
                "            \"Credits_c\"\n" +
                "        ],\n" +
                "        \"criteria\": {\n" +
                "            \"field\": {\n" +
                "                \"api_name\": \"id\"\n" +
                "            },\n" +
                "            \"comparator\": \"in\",\n" +
                "            \"value\": [" + idsStr + "]\n" +
                "        }\n" +
                "    }\n" +
                "}";
        log.info("查询目标医院");
        List<CsvRow> data = BulkUtil.getBulkCSVData(token, query, new ArrayList<>());

        List<JSONObject> result_JsonObjects = new ArrayList<>();
        for (CsvRow csvRow : data) {
            if (StrUtil.isNotBlank(csvRow.get(0)) && !"Id".equals(csvRow.get(0))) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("id", csvRow.get(0));
                JSONObject temp = new JSONObject();
                temp.put("name", csvRow.get(1));
                jsonObject.put("HospitalList_c", temp);
                jsonObject.put("Credits_c", csvRow.get(2));
                result_JsonObjects.add(jsonObject);
            }
        }

        return result_JsonObjects;
    }

    //删除目录下面所有的文件
    public static boolean deleteContents(File dir) {
        File[] files = dir.listFiles();
        boolean success = true;
        if (files != null) {
            for (File file : files) {
                if (file.isDirectory()) {
                    success &= deleteContents(file);
                }
                if (!file.delete()) {
                    System.out.println("Failed to delete " + file);
                    success = false;
                }
            }
        }
        return success;
    }
}
